package com.briup.smartlab.config;

import com.briup.smartlab.web.interceptor.JwtInterceptor;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * web层配置类
 * @Author briup
 */
//@Configuration //配置类
public class WebConfig  implements WebMvcConfigurer {
    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //2.将身份验证的拦截器添加到程序中,不拦截 静态资源 登录 注册 首页信息 等接口
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                //不拦截登录请求和swagger页面
                .excludePathPatterns("/swagger-ui/**",
                                    "/swagger-resources/**",
                                    "/v3/**",
                                    "/login",
                                    "/loginout");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
       //设置跨域访问
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
                .allowedHeaders("*")
                .maxAge(86400)
                .allowCredentials(false);
    }
    @Bean
    public DefaultKaptcha producer(){
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        return defaultKaptcha;
    }
}
